package com.sreader.sreader4j;

import java.util.ArrayList;
import java.util.Locale;

import com.sreader.logic.InterpretationLogic;
import com.sreader.logic.NombresCalculer.nombreType;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.FooterClickEvent;
import com.vaadin.ui.Table.HeaderClickEvent;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

public class FeuilleDeResultat extends CustomComponent {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Window mainWindow;
	private Window resultatWindow;
	private Panel mainPanel;
	final int RESULTATId = 0;
	final int RESULTATEXTRAId = 1;
	//@AutoGenerated
	private AbsoluteLayout mainLayout;
	//@AutoGenerated
	private AbsoluteLayout resultatAbsoluteLayout;
	private Table tableDeResultat;
	private Table tableDesExtras;
	private Locale currentLocaleValue;
	private InterpretationLogic interpret;
	private String detailsMessageString;
	private Label resultatContentLabel;
	private  String columnId;
	private String firstNames;
	private String names;
	private String birthDateAsString;
	private Messages messages;
	
	
	public String getFirstNames() {
		return firstNames;
	}
	public void setFirstNames(String firstNames) {
		this.firstNames = firstNames;
		interpret.setIndividumFirstNames(getFirstNames());
	}
	public String getNames() {
		return names;
	}
	public void setNames(String names) {
		this.names = names;
		interpret.setIndividumNames(getNames());
	}
	public String getBirthDateAsString() {
		return birthDateAsString;
	}
	public void setBirthDateAsString(String birthDateAsString) {
		this.birthDateAsString = birthDateAsString;
		interpret.setIndividumBirthDateAsString(birthDateAsString);
	}
	

	public Messages getMessages() {
		return messages;
	}


	public void setMessages(Messages messages) {
		this.messages = messages;
	}
	
	private void setCurrentLocaleValue(Locale currentLocaleValue) {
		this.currentLocaleValue = currentLocaleValue;
	}
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public FeuilleDeResultat(Locale defaultLocale, Window main) {
		setCurrentLocaleValue(defaultLocale);
		setMessages(new Messages("FeuilleDeResultat",defaultLocale));
		mainWindow = main;
		buildMainLayout();
		setCompositionRoot(mainLayout);
		interpret = new InterpretationLogic(defaultLocale,"FeuilleDeResultat");
		columnId = "";
		// TODO add user code here
	}
	
	private Panel buildMainPanel() {
		// common part: create layout
		mainPanel = new Panel(this.messages.getString("feuilleCaption"));
		mainPanel.setImmediate(true);
		mainPanel.setWidth("550.0px"); //720.0
		mainPanel.setHeight("450px"); //900.0
		mainPanel.getContent().setSizeUndefined();
		// mainPanelAbsoluteLayout
		//mainPanelAbsoluteLayout = buildMainPanelAbsoluteLayout();
		mainPanel.setContent(resultatAbsoluteLayout);
		//mainPanel.setScrollable(true);
		
		return mainPanel;
	}
	
	private AbsoluteLayout buildResultatAbsoluteLayout(){
		resultatAbsoluteLayout = new AbsoluteLayout();
		resultatAbsoluteLayout.setImmediate(true);
		resultatAbsoluteLayout.setWidth("510.0px");
		resultatAbsoluteLayout.setHeight("410.0px");
		//resultatAbsoluteLayout.setSizeFull();
		resultatContentLabel = new Label();
		resultatContentLabel.setImmediate(true);
		resultatContentLabel.setContentMode(Label.CONTENT_RAW);
		resultatContentLabel.setWidth("500.0px");
		resultatContentLabel.setHeight("400.0px");
		resultatAbsoluteLayout.addComponent(resultatContentLabel, 
				"top:0.0px;left:2.0px;");
		
		return resultatAbsoluteLayout;
	}
	public void buildResultWindow(){
		resultatWindow = new Window();
		resultatWindow.setImmediate(true);
		resultatWindow.setPositionX(100);
		resultatWindow.setPositionY(250);
		mainWindow.addWindow(resultatWindow);
	}
	//@AutoGenerated
	private AbsoluteLayout buildMainLayout() {
		
		 detailsMessageString = this.messages.getString("details");
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setImmediate(true);
		
		mainLayout.setWidth("500px");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("500.0px");
		setHeight("300.0px");
		
		
		
		// tableDeResultat
		tableDeResultat = new Table();
		tableDeResultat.setImmediate(true);
		tableDeResultat.setWidth("100%");
		
		
		
		//tableDeResultat.setHeight("100");
		tableDeResultat.setPageLength(1);
		tableDeResultat.setFooterVisible(true);
		tableDeResultat.addContainerProperty("NA", Integer.class, null, "NA", null, Table.ALIGN_LEFT);
		tableDeResultat.setColumnFooter("NA", detailsMessageString);
		tableDeResultat.setColumnWidth("NA", 52);
		tableDeResultat.addContainerProperty("NH", Integer.class, null, "NH", null, Table.ALIGN_LEFT);
		tableDeResultat.setColumnFooter("NH", detailsMessageString);
		tableDeResultat.setColumnWidth("NH", 52);
		tableDeResultat.addContainerProperty("NE", Integer.class, null, "NE", null, Table.ALIGN_LEFT);
		tableDeResultat.setColumnFooter("NE", detailsMessageString);
		tableDeResultat.setColumnWidth("NE", 52);
		tableDeResultat.addContainerProperty("NI", Integer.class, null, "NI", null, Table.ALIGN_LEFT);
		tableDeResultat.setColumnFooter("NI", detailsMessageString);
		tableDeResultat.setColumnWidth("NI", 52);
		tableDeResultat.addContainerProperty("NR", Integer.class, null, "NR", null, Table.ALIGN_LEFT);
		tableDeResultat.setColumnFooter("NR", detailsMessageString);
		tableDeResultat.setColumnWidth("NR", 52);
		tableDeResultat.addContainerProperty("NDI", Integer.class, null, "NDI", null, Table.ALIGN_LEFT);
		tableDeResultat.setColumnFooter("NDI", detailsMessageString);
		tableDeResultat.setColumnWidth("NDI", 52);
		tableDeResultat.addContainerProperty("CV", Integer.class, null, "CV", null, Table.ALIGN_LEFT);
		tableDeResultat.setColumnFooter("CV", detailsMessageString);
		tableDeResultat.setColumnWidth("CV", 52);
		tableDeResultat.addContainerProperty("NU", Integer.class, null, "NU", null, Table.ALIGN_LEFT);
		tableDeResultat.setColumnFooter("NU", detailsMessageString);
		tableDeResultat.setColumnWidth("NU", 55);
		//tableDeResultat.setPageLength(tableDeResultat.size());

		tableDeResultat.setSortDisabled(true);
		tableDeResultat.setEditable(false);
		tableDeResultat.setColumnReorderingAllowed(false);

		tableDeResultat.addListener(new Table.HeaderClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void headerClick(HeaderClickEvent event) {
				// TODO Auto-generated method stub
				buildResultatAbsoluteLayout();
				buildMainPanel();
				columnId = (String) event.getPropertyId();
				try{
					if(tableDeResultat.getContainerProperty(RESULTATId, columnId).getValue().toString() == null){
						getWindow().showNotification("no resulError");
					}
					resultatWindow = new Window();
					resultatWindow.setPositionX(150);
					resultatWindow.setPositionY(250);
					mainWindow.setScrollable(false);
					getWindow().showNotification(columnId, interpret.setDescription(getNombreType(columnId)), Notification.TYPE_HUMANIZED_MESSAGE,true);				
				}catch (Exception e) {
					// TODO: handle exception no resulError
					getWindow().showNotification("no resulError");
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		tableDeResultat.addListener(new Table.FooterClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void footerClick(FooterClickEvent event) {
				// TODO Auto-generated method stub
				buildResultatAbsoluteLayout();
				buildMainPanel();
				columnId = (String) event.getPropertyId();
				
				try{
					if(tableDeResultat.getContainerProperty(RESULTATId, columnId).getValue().toString() == null){
						getWindow().showNotification("no resulError");
					}
					resultatWindow = new Window();
					resultatWindow.setPositionX(150);
					resultatWindow.setPositionY(250);
					mainWindow.addWindow(resultatWindow);
					mainWindow.setScrollable(false);
					//getWindow().showNotification(columnId);

					if ( nombreType.NA.toString().equalsIgnoreCase(columnId)){
						
						int valueNA = Integer.parseInt(tableDeResultat.getContainerProperty(RESULTATId, columnId).getValue().toString());

						resultatWindow.setContent(mainPanel);
						resultatContentLabel.setValue(interpret.interpretSingleResult(valueNA, nombreType.NA));
						resultatContentLabel.requestRepaint();
					}
					if (nombreType.NE.toString().equalsIgnoreCase(columnId)){
						int valueNE = Integer.parseInt(tableDeResultat.getContainerProperty(RESULTATId, columnId).getValue().toString());
						resultatWindow.setContent(mainPanel);
						resultatContentLabel.setValue(interpret.interpretSingleResult(valueNE, nombreType.NE));
						resultatContentLabel.requestRepaint();
					}
					if (nombreType.NH.toString().equalsIgnoreCase(columnId)){
						int valueNH = Integer.parseInt(tableDeResultat.getContainerProperty(RESULTATId, columnId).getValue().toString());
						resultatWindow.setContent(mainPanel);
						resultatContentLabel.setValue(interpret.interpretSingleResult(valueNH, nombreType.NH));
						resultatContentLabel.requestRepaint();
					}
				}catch (Exception e) {
					// TODO: handle exception
					getWindow().showNotification("no resulError");
				}
				
			}
		});
		mainLayout.addComponent(tableDeResultat, "top:0.0px;left:0.0px;");
		
		// tableDesExtras
		tableDesExtras = new Table();
		tableDesExtras.setImmediate(true);
		tableDesExtras.setWidth("100%");
		//tableDesExtras.setHeight("100px");
		tableDesExtras.setFooterVisible(true);
		tableDesExtras.setSortDisabled(true);
		tableDesExtras.setPageLength(1);
		tableDesExtras.addContainerProperty("NAbscents", ArrayList.class, null, "NAbscents", null, Table.ALIGN_LEFT);
		tableDesExtras.setColumnFooter("NAbscents", detailsMessageString);
		tableDesExtras.setColumnWidth("NAbscents", 105);
		tableDesExtras.addContainerProperty("NDominants", ArrayList.class, null, "NDominants", null, Table.ALIGN_LEFT);
		tableDesExtras.setColumnFooter("NDominants", detailsMessageString);
		tableDesExtras.setColumnWidth("NDominants", 105);
		tableDesExtras.addContainerProperty("NExces", ArrayList.class, null, "NExces", null, Table.ALIGN_LEFT);
		tableDesExtras.setColumnFooter("NExces", detailsMessageString);
		tableDesExtras.setColumnWidth("NExces", 105);
		tableDesExtras.addContainerProperty("NAP", Integer.class, null,"NAP",null,Table.ALIGN_LEFT);
		tableDesExtras.setColumnFooter("NAP", detailsMessageString);
		tableDesExtras.setColumnWidth("NAP", 45);
		tableDesExtras.addContainerProperty("NMP", Integer.class, null,"NMP",null,Table.ALIGN_LEFT);
		tableDesExtras.setColumnFooter("NMP", detailsMessageString);
		tableDesExtras.setColumnWidth("NMP", 45);
		tableDesExtras.addContainerProperty("NJP", Integer.class, null,"NJP",null,Table.ALIGN_LEFT);
		tableDesExtras.setColumnFooter("NJP", detailsMessageString);
		tableDesExtras.setColumnWidth("NJP", 45);
		//tableDesExtras.setPageLength(tableDesExtras.size());
		tableDesExtras.setEditable(false);
		tableDesExtras.setColumnReorderingAllowed(false);
		
		tableDesExtras.addListener(new Table.HeaderClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void headerClick(HeaderClickEvent event) {
				// TODO Auto-generated method stub
				buildResultatAbsoluteLayout();
				buildMainPanel();
				String columnId = (String) event.getPropertyId();
				//getWindow().showNotification(columnId);
				try{
					//resultatWindow = new Window();
					resultatWindow.setPositionX(150);
					resultatWindow.setPositionY(250);
					mainWindow.setScrollable(false);
					getWindow().showNotification(columnId, interpret.setDescription(getNombreType(columnId)), Notification.TYPE_HUMANIZED_MESSAGE,true);
				}
				catch (Exception e) {
				// TODO: handle exception
					getWindow().showNotification("no resulError");
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}});
		tableDesExtras.addListener(new Table.FooterClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void footerClick(FooterClickEvent event) {
				// TODO Auto-generated method stub
				buildResultatAbsoluteLayout();
				buildMainPanel();
				resultatWindow = new Window();
				resultatWindow.setPositionX(150);
				resultatWindow.setPositionY(250);
				mainWindow.addWindow(resultatWindow);
				mainWindow.setScrollable(false);
				columnId = (String) event.getPropertyId();
				if (nombreType.NJP.toString().equalsIgnoreCase(columnId)){
					int valueNJP = Integer.parseInt(tableDesExtras.getContainerProperty(RESULTATId, columnId).getValue().toString());
					resultatWindow.setContent(mainPanel);
					resultatContentLabel.setValue(interpret.interpretSingleResult(valueNJP, nombreType.NJP));
					resultatContentLabel.requestRepaint();
				}
				if (nombreType.NMP.toString().equalsIgnoreCase(columnId)){
					int valueNMP = Integer.parseInt(tableDesExtras.getContainerProperty(RESULTATId, columnId).getValue().toString());
					resultatWindow.setContent(mainPanel);
					resultatContentLabel.setValue(interpret.interpretSingleResult(valueNMP, nombreType.NMP));
					resultatContentLabel.requestRepaint();
				}
			}
		});	
		
		mainLayout.addComponent(tableDesExtras, "top:125px;left:0.0px;");
		return mainLayout;
	}
	
	
	
	
	public void setTableDeResultat(int na,int nh, int ne, int ni, int nr, int ndi,
						int cv, int nu, ArrayList<Integer>nabsents,
						ArrayList<Integer>ndominants, ArrayList<Integer>nexces,
						int nap,int nmp,int njp){
		resetTableDeResultat();
		this.tableDeResultat.addItem(new Object[]{
				na,nh,ne,ni,nr,ndi,cv,nu
		},RESULTATId);
		
		this.tableDesExtras.addItem(new Object[]{
				nabsents,ndominants,nexces,nap,nmp,njp
		},RESULTATId);
			
	}
	
	public void resetTableDeResultat(){
		this.tableDeResultat.removeAllItems();
		this.tableDesExtras.removeAllItems();
	}
	/*
	 *
	 */
	private nombreType getNombreType (String columnId) throws Exception{
		if(columnId == null){
			throw new Exception("This method need a columnId");
		}
		if(columnId.equalsIgnoreCase(nombreType.NA.toString())){
			return nombreType.NA;
		}
		if(columnId.equalsIgnoreCase(nombreType.NAbscents.toString())){
			return nombreType.NAbscents;
		}
		if(columnId.equalsIgnoreCase(nombreType.NAP.toString())){
			return nombreType.NAP;
		}
		if(columnId.equalsIgnoreCase(nombreType.NCroise.toString())){
			return nombreType.NCroise;
		}
		if(columnId.equalsIgnoreCase(nombreType.NDI.toString())){
			return nombreType.NDI;
		}
		if(columnId.equalsIgnoreCase(nombreType.NDominants.toString())){
			return nombreType.NDominants;
		}
		if(columnId.equalsIgnoreCase(nombreType.NE.toString())){
			return nombreType.NE;
		}
		if(columnId.equalsIgnoreCase(nombreType.NExces.toString())){
			return nombreType.NExces;
		}
		if(columnId.equalsIgnoreCase(nombreType.NH.toString())){
			return nombreType.NH;
		}
		if(columnId.equalsIgnoreCase(nombreType.NI.toString())){
			return nombreType.NI;
		}
		if(columnId.equalsIgnoreCase(nombreType.NJP.toString())){
			return nombreType.NJP;
		}if(columnId.equalsIgnoreCase(nombreType.NMP.toString())){
			return nombreType.NMP;
		}
		if(columnId.equalsIgnoreCase(nombreType.NR.toString())){
			return nombreType.NR;
		}
		if(columnId.equalsIgnoreCase(nombreType.NU.toString())){
			return nombreType.NU;
		}
		if(columnId.equalsIgnoreCase(nombreType.CV.toString())){
			return nombreType.CV;
		}	
		return null;
	}
}
